home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / PropertyChangeSupport.java < prev    next >
Text File  |  1998-08-21  |  5KB  |  159 lines

  1. /*
  2.  * @(#)PropertyChangeSupport.java    1.0 5/6/97
  3.  *
  4.  * Copyright (c) 1997 Symantec, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.beans;
  9.  
  10. import java.io.Serializable;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. import java.beans.PropertyVetoException;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.VetoableChangeListener;
  17. import java.beans.PropertyChangeEvent;
  18.  
  19.  
  20.  
  21. //    05/06/97    LAB    Created
  22.  
  23. /**
  24.  * This is a utility class that can be used by beans that support bound
  25.  * properties.  You can either inherit from this class or you can use
  26.  * an instance of this class as a member field of your bean and delegate
  27.  * various work to it.
  28.  * <p>
  29.  * This extension of the java.beans.PropertyChangeSupport class adds
  30.  * functionality to handle individual property changes.
  31.  *
  32.  * @author Symantec
  33.  */
  34. public class PropertyChangeSupport extends java.beans.PropertyChangeSupport implements java.io.Serializable
  35. {
  36.     /**
  37.      * Constructs a PropertyChangeSupport object.
  38.      * @param sourceBean the bean to be given as the source for any events
  39.      */
  40.     public PropertyChangeSupport(Object sourceBean)
  41.     {
  42.         super(sourceBean);
  43.         source = sourceBean;
  44.     }
  45.  
  46.     /**
  47.      * Adds a PropertyChangeListener to the listener list.
  48.      *
  49.      * @param propertyName the name of the property to add a listener for
  50.      * @param listener the PropertyChangeListener to be added
  51.      * @see #removePropertyChangeListener
  52.      */
  53.     public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
  54.     {
  55.         java.util.Vector listenerList;
  56.  
  57.         if(listenerTable == null)
  58.         {
  59.             listenerTable = new java.util.Hashtable();
  60.         }
  61.  
  62.         if(listenerTable.containsKey(propertyName))
  63.         {
  64.             listenerList = (java.util.Vector)listenerTable.get(propertyName);
  65.         }
  66.         else
  67.         {
  68.             listenerList = new java.util.Vector();
  69.         }
  70.  
  71.         listenerList.addElement(listener);
  72.         listenerTable.put(propertyName, listenerList);
  73.     }
  74.  
  75.     /**
  76.      * Removes a PropertyChangeListener from the listener list.
  77.      *
  78.      * @param propertyName the name of the property to remove a listener for
  79.      * @param listener the PropertyChangeListener to be removed
  80.      * @see #addPropertyChangeListener
  81.      */
  82.     public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
  83.     {
  84.         java.util.Vector listenerList;
  85.  
  86.         if (listenerTable == null || !listenerTable.containsKey(propertyName))
  87.         {
  88.             return;
  89.         }
  90.         listenerList = (java.util.Vector)listenerTable.get(propertyName);
  91.         listenerList.removeElement(listener);
  92.     }
  93.  
  94.     /**
  95.      * Report a bound property update to any registered listeners.
  96.      * <p>
  97.      * No event is fired if old and new are equal and non-null.
  98.      *
  99.      * @param propertyName the programmatic name of the property
  100.      *        that was changed
  101.      * @param oldValue the old value of the property
  102.      * @param newValue the new value of the property
  103.      */
  104.     public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
  105.     {
  106.         if (oldValue != null && oldValue.equals(newValue))
  107.         {
  108.             return;
  109.         }
  110.  
  111.         super.firePropertyChange(propertyName, oldValue, newValue);
  112.  
  113.         java.util.Hashtable templistenerTable = null;
  114.  
  115.         synchronized (this)
  116.         {
  117.             if(listenerTable == null || !listenerTable.containsKey(propertyName))
  118.             {
  119.                 return;
  120.             }
  121.               templistenerTable = (java.util.Hashtable) listenerTable.clone();
  122.         }
  123.  
  124.         java.util.Vector listenerList;
  125.  
  126.         listenerList = (java.util.Vector)templistenerTable.get(propertyName);
  127.  
  128.         PropertyChangeEvent evt = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
  129.  
  130.         for (int i = 0; i < listenerList.size(); i++)
  131.         {
  132.             PropertyChangeListener target = (PropertyChangeListener)listenerList.elementAt(i);
  133.             target.propertyChange(evt);
  134.         }
  135.     }
  136.  
  137.     /* !!! LAB !!!    05/06/97
  138.     If we want to support non-serializable listeners we will have to
  139.     implement the folowing functions and serialize out the listenerTable
  140.     HashTable on our own.
  141.  
  142.     private void writeObject(ObjectOutputStream s) throws IOException
  143.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
  144.     */
  145.  
  146.     /**
  147.      * The listener list.
  148.      * @see #addPropertyChangeListener
  149.      * @see #removePropertyChangeListener
  150.      */
  151.     protected java.util.Hashtable listenerTable;
  152.     private Object source;
  153.     private int propertyChangeSupportSerializedDataVersion = 1;
  154. }
  155.  
  156.  
  157.  
  158.  
  159.